<?php

namespace app\shop\service\statistics;

use app\shop\model\user\Favorite as FavoriteModel;
use app\shop\model\user\Visit as VisitModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 * 供应商数据概况
 */
class AccessService
{
	/**
	 * 获取数据概况
	 */
	public function getData()
	{
		$today = date('Y-m-d');
		$yesterday = date('Y-m-d', strtotime('-1 day'));
		$data = [
			// 供应商收藏数
			'supplier_fav_total' => [
				'today' => number_format($this->getFavData(null, $today, 'supplier')),
				'yesterday' => number_format($this->getFavData(null, $yesterday, 'supplier'))
			],
			// 商品收藏数
			'product_fav_total' => [
				'today' => number_format($this->getFavData(null, $today, 'product')),
				'yesterday' => number_format($this->getFavData(null, $yesterday, 'product'))
			],
			// 访客数
			'visit_user' => [
				'today' => number_format($this->getVisitData($today, null, 'visit_user')),
				'yesterday' => number_format($this->getVisitData($yesterday, null, 'visit_user'))
			],
			// 访问量
			'visit_count' => [
				'today' => number_format($this->getVisitData($today, null, 'visit_count')),
				'yesterday' => number_format($this->getVisitData($yesterday, null, 'visit_count'))
			],
		];
		return $data;
	}

	/**
	 * 通过时间段查询用户数据
	 */
	public function getVisitDataByDate($days)
	{
		$data = [];
		foreach ($days as $day) {
			$data[] = [
				'day' => $day,
				'visit_user' => $this->getVisitData($day, null, 'visit_user'),
				'visit_count' => $this->getVisitData($day, null, 'visit_count')
			];
		}
		return $data;
	}

	/**
	 * 通过时间段查询用户数据
	 */
	public function getFavDataByDate($days)
	{
		$data = [];
		foreach ($days as $day) {
			$data[] = [
				'day' => $day,
				'supplier_count' => $this->getFavData($day, null, 'supplier'),
				'product_count' => $this->getFavData($day, null, 'product')
			];
		}
		return $data;
	}

	/**
	 * 获取用户数据
	 */
	private function getVisitData($startDate, $endDate, $type)
	{
		return (new VisitModel())->getVisitData($startDate, $endDate, $type);
	}

	/**
	 * 收藏数
	 */
	private function getFavData($startDate, $endDate, $type)
	{
		return (new FavoriteModel())->getFavData($startDate, $endDate, $type);
	}

	/**
	 * 按日期导出
	 */
	public function getSearchData($datetime,$endtime=0)
    {
        $today = date('Y-m-d',$datetime);
        if(empty($endtime)){
        	$yesterday = date('Y-m-d', strtotime('+1 day',$datetime));
        }else{
        	$yesterday = date('Y-m-d 23:59:59',$endtime);
        }
        return (new VisitModel())->getVisitDataList($today, $yesterday);
    }

    /**
     * 导出
     */
    public function ExportListBlack($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //列宽
        $sheet->getColumnDimension('C')->setWidth(30);
        $sheet->getColumnDimension('D')->setWidth(30);

        //设置工作表标题名称
        $sheet->setTitle('访客明细');

        $sheet->setCellValue('A1', '#');
        $sheet->setCellValue('B1', '用户ID');
        $sheet->setCellValue('C1', '用户昵称');
        $sheet->setCellValue('D1', '手机号');
        $sheet->setCellValue('E1', '访问');
        $sheet->setCellValue('F1', '日期');
        $sheet->setCellValue('G1', '时间');

        //填充数据
        $index = 0;
        foreach ($list as $key=>$order) {
            // $sheet->setCellValue('A' . ($index + 2), "\t" . $order['order_no'] . "\t");
            $sheet->setCellValue('A' . ($index + 2), "\t" . ($key+1) . "\t");
            $sheet->setCellValue('B' . ($index + 2), $order['visiter_id']);
            $sheet->setCellValue('C' . ($index + 2), $order['visiter']);
            $sheet->setCellValue('D' . ($index + 2), "\t" . $order['visiter_phone'] . "\t");
            $sheet->setCellValue('E' . ($index + 2), "商品");
            $sheet->setCellValue('F' . ($index + 2), $order['day']);
            $sheet->setCellValue('G' . ($index + 2), $order['time']);
            $index++;
        }

        //保存文件
        $writer = new Xlsx($spreadsheet);
        $filename = iconv("UTF-8", "GB2312//IGNORE", '访问') . '-' . date('YmdHis') . '.xlsx';


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit();
    }
}